﻿using AspNet.Identity.Dapper.Db;
using Dapper;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AspNet.Identity.Dapper
{
    public class UserLoginsTable
    {
        private readonly DbManager _db;
        public UserLoginsTable(DbManager dbManager)
        {
            _db = dbManager;
        }

        public bool Delete(IdentityUser user, UserLoginInfo login)
        {
            return _db.Connection.Execute(@"Delete from UserLogin 
                    where UserId = @userId 
                    and LoginProvider = @loginProvider 
                    and ProviderKey = @providerKey",
                new
                {
                    userId = user.Id.ToString(),
                    loginProvider = login.LoginProvider,
                    providerKey = login.ProviderKey
                })>0;
        }

        /// <summary>
        /// Deletes all Logins from a user in the UserLogins table
        /// </summary>
        /// <param name="userId">The user's id</param>
        /// <returns></returns>
        public bool Delete(Guid userId)
        {
            var id = userId.ToString();
             return _db.Connection.Execute(@"Delete from UserLogin 
                    where UserId = @userId", new { userId })>0;
        }

        /// <summary>
        /// Inserts a new login in the UserLogins table
        /// </summary>
        /// <param name="user">User to have new login added</param>
        /// <param name="login">Login to be added</param>
        /// <returns></returns>
        public bool Insert(IdentityUser user, UserLoginInfo login)
        {
            return _db.Connection.Execute(@"Insert into UserLogin 
                (LoginProvider, ProviderKey, UserId) 
                values (@loginProvider, @providerKey, @userId)",
                    new
                    {
                        loginProvider = login.LoginProvider,
                        providerKey = login.ProviderKey,
                        userId = user.Id.ToString()
                    })>0;
        }

        /// <summary>
        /// Return a userId given a user's login
        /// </summary>
        /// <param name="MemberLogin">The user's login info</param>
        /// <returns></returns>
        public Guid FindUserIdByLogin(UserLoginInfo userLogin)
        {
            return _db.Connection.ExecuteScalar<Guid>(@"Select UserId from UserLogin 
                where LoginProvider = @loginProvider and ProviderKey = @providerKey",
                        new
                        {
                            loginProvider = userLogin.LoginProvider,
                            providerKey = userLogin.ProviderKey
                        });
        }

        /// <summary>
        /// Returns a list of user's logins
        /// </summary>
        /// <param name="userId">The user's id</param>
        /// <returns></returns>
        public List<UserLoginInfo> FindByUserId(Guid userId)
        {
            return _db.Connection.Query<UserLoginInfo>("Select LoginProvider,ProviderKey from UserLogin where userId = @userId", new { userId = userId.ToString() })
                .ToList();
        }
    }
}
